package com.xm.xmapp.fragment;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.amap.api.maps.AMapUtils;
import com.android.volley.VolleyError;
import com.xm.xmapp.LongRentOrderYuyueActivity;
import com.xm.xmapp.MainActivity;
import com.xm.xmapp.R;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.app.Type;
import com.xm.xmapp.base.BaseFragment;
import com.xm.xmapp.map.ClusterItem;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.myinterface.MainActivityInterFace;
import com.xm.xmapp.myinterface.ScrollInterFaceAble;
import com.xm.xmapp.request.bean.Get_network_car_genre_listRequest;
import com.xm.xmapp.response.bean.CustomHeightBean;
import com.xm.xmapp.response.bean.Get_network_car_genre_listResponse;
import com.xm.xmapp.response.bean.ResponseLongRentOrder;
import com.xm.xmapp.response.bean.ResponsePlieNetworkInfo;
import com.xm.xmapp.response.bean.ResponseUserBean;
import com.xm.xmapp.tools.StringTools;
import com.xm.xmapp.tools.Tools;


public class LongTimeCarInfoFragment extends BaseFragment implements OnClickListener{

    TextView tvAddress;
    TextView tvDistance;
    TextView tvOpeningHours;
    RelativeLayout rlBtn;
    LinearLayout bottom;
    LinearLayout layout;


    private String str;
    private ResponsePlieNetworkInfo base;
    private ResponseLongRentOrder get_longrent_order_infoResponse;
    private Rect mRect;
    ResponseUserBean userbean;
    Intent intent;

    public LongTimeCarInfoFragment() {

    }

    public static LongTimeCarInfoFragment newInstance(ResponseLongRentOrder get_longrent_order_infoResponse,
                                                      ClusterItem item, ResponsePlieNetworkInfo base) {
        LongTimeCarInfoFragment newFragment = new LongTimeCarInfoFragment();
        newFragment.setArguments(new Bundle());
        String str;
        if (MyAppcation.getMyAppcation().getMyLocation() != null)
            str = StringTools.getJuliString(AMapUtils.calculateLineDistance(MyAppcation.getMyAppcation()
                    .getMyLocation(), item.getLatLng()) / 1000)
                    + "km";
        else
            str = "0";
        newFragment.setData(get_longrent_order_infoResponse, str, base);
        return newFragment;

    }

    public void setData(ResponseLongRentOrder get_longrent_order_infoResponse, String str, ResponsePlieNetworkInfo base) {
        this.base = base;
        this.get_longrent_order_infoResponse = get_longrent_order_infoResponse;
        this.str = str;
    }

    @Override
    public int setContentView() {
        return R.layout.fragment_longrent_carinfo;
    }

    @Override
    public void initView() {
        tvAddress = (TextView) findViewById(R.id.tv_address);
        tvDistance = (TextView) findViewById(R.id.tv_distance);
        tvOpeningHours = (TextView) findViewById(R.id.tv_opening_hours);
        rlBtn = (RelativeLayout) findViewById(R.id.rl_btn);
        layout = (LinearLayout) findViewById(R.id.layout);
        bottom = (LinearLayout) findViewById(R.id.bottom);

    }

    @Override
    public void initData() {
        tvAddress.setText(base.network_name);
        tvDistance.setText("距离:" + str);
        tvOpeningHours.setText("营业时间:" + base.opening_hours);

        if (get_longrent_order_infoResponse != null && get_longrent_order_infoResponse.bean != null) {
            rlBtn.setVisibility(View.INVISIBLE);
        } else {
            rlBtn.setVisibility(View.VISIBLE);
        }

    }

    @Override
    public void ViewTreeObserver() {
        super.ViewTreeObserver();
        CustomHeightBean bean = new CustomHeightBean();
        bean.ExitOffset = layout.getHeight();
        bean.MaxOffset = layout.getHeight();
//        initRect();
        bean.flag = MainActivityInterFace.INTT_STATUS;
        MyAppcation.getMyAppcation().notifyDataSetChanged(MainActivity.class, Type.A_Main.CHANGE_UI_CODE, bean);

    }

    @Override
    public void initEvent() {
        rlBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View arg0) {
        switch (arg0.getId()) {
            case R.id.rl_btn:
                getData();
                break;
            default:
                break;
        }
    }

    private void getData() {
        promptDialog.show();
        Get_network_car_genre_listRequest requestBean = new Get_network_car_genre_listRequest();
        requestBean.network_id = base.network_id;
        MyAppcation.getMyAppcation().getPostData(getActivity(), requestBean, new GetDataInterFace<String>() {

            @Override
            public void onResponse(String response) {
                promptDialog.dismiss();
                Get_network_car_genre_listResponse bean = JSON.parseObject(response,
                        Get_network_car_genre_listResponse.class);

                if (bean.carGenreList == null || bean.carGenreList.size() == 0) {
                    Tools.showMessage("该网点暂无车型");
                } else {
                    intent = new Intent(MyAppcation.getMyAppcation(), LongRentOrderYuyueActivity.class);
                    intent.putExtra("network_id", base.network_id);
                    intent.putExtra("network_name", base.network_name);
                    intent.putExtra("bean", JSON.toJSONString(bean));
                    startActivity(intent);
                }
            }
        }, new DisssmissInterFace() {

            @Override
            public void putError(VolleyError error) {
                promptDialog.dismiss();
            }
        });

    }


//    @Override
//    public void setOffext(int currentProgress) {
//        if(rlBtn!=null && mRect!=null) {
//            rlBtn.layout(mRect.left, -currentProgress + mRect.top, mRect.right, mRect.bottom - currentProgress);
//        }
//    }

//    @Override
//    public void initRect() {
//        if(mRect==null) {
//            int left = rlBtn.getLeft();
//            int top = rlBtn.getTop();
//            int right = rlBtn.getRight();
//            int bottom = rlBtn.getBottom();
//            mRect = new Rect(left, top, right, bottom);
//        }
//    }
}
